package com.foreknow.tijian.dao.Impl;

import com.foreknow.tijian.dao.ordersDao;
import com.foreknow.tijian.po.Hospital;
import com.foreknow.tijian.po.Orders;
import com.foreknow.tijian.po.Setmeal;
import com.foreknow.tijian.po.Users;
import com.foreknow.tijian.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class OrdersDaoImpl implements ordersDao {
    //连接数据库
    Connection con= DBUtil.getConnection();
    PreparedStatement pst;
    int state;
    @Override
    public int getOrdersByUserId(Orders order) {
    String sql="select state from orders where userId=?";
    try{
        //获取与处理对象
        pst=con.prepareStatement(sql);
        pst.setString(1, order.getUserId());
        ResultSet rs=pst.executeQuery();
        if(rs.next()){
            //获取值
            state=rs.getInt(1);
        }
    }
    catch (
    SQLException e) {
        throw new RuntimeException(e);
    }
        finally {
        //关闭资源
        DBUtil.close(pst);
    }
    return state;
    }

    @Override
    public int saveOrders(Orders order) {
        int result=0;
        String sql="insert into orders values(?,?,?,?,?,1)";
        try{
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, order.getOrderId());
            pst.setString(2, order.getOrderDate());
            pst.setString(3,order.getUserId());
            pst.setInt(4,order.getHpid());
            pst.setInt(5,order.getSmid());
            result = pst.executeUpdate();
        }
        catch (SQLException e) {
            throw new RuntimeException(e);
        }
        finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public int removeOrders(Orders order) {
        int result=0;
        String sql="delete from orders where orderId=?";
        try{
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, order.getOrderId());
            result = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public Orders getOrdersById(Orders order) {
        Orders orders1=new Orders();
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT o.orderDate oorderDate, ");
        sql.append("u.realName uname, ");
        sql.append("u.identityCard uidentityCard, ");
        sql.append("u.birthday  ubirthday, ");
        sql.append("u.userId uuserId, ");
        sql.append("h.name hname, ");
        sql.append("h.businessHours hbusinessHours, ");
        sql.append("h.deadline hdeadline, ");
        sql.append("h.telephone htelephone, ");
        sql.append("h.address haddress, ");
        sql.append("s.name sname ");
        sql.append("FROM orders o left JOIN users u ON u.userId=o.userId ");
        sql.append("LEFT JOIN hospital h ON o.hpId = h.hpId ");
        sql.append("LEFT JOIN setmeal s ON o.smId=s.smId ");
        sql.append("where o.orderId=?");
        try{
            //获取与处理对象
            pst=con.prepareStatement(sql.toString());
            pst.setString(1, order.getOrderId());
            ResultSet rs=pst.executeQuery();
            if(rs.next()){
               orders1.setOrderDate(rs.getString("oorderDate"));
                Users user=new Users();
                user.setRealName(rs.getString("uname"));
                user.setIdentityCard(rs.getString("uidentityCard"));
                user.setBirthday((rs.getDate("ubirthday")));
                user.setUserId(rs.getString("uuserId"));
                Hospital hospital=new Hospital();
                hospital.setName(rs.getString("hname"));
                hospital.setBusinessHours(rs.getString("hbusinessHours"));
                hospital.setDeadline(rs.getString("hdeadline"));
                hospital.setTelephone(rs.getString("htelephone"));
                hospital.setAddress(rs.getString("haddress"));
                Setmeal setmeal=new Setmeal();
                setmeal.setName(rs.getString("sname"));
                orders1.setUser(user);
                orders1.setHospital(hospital);
                orders1.setSetmeal(setmeal);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(pst);
        }
        return orders1;
    }
    @Override
    public List<Orders> listOrdersByUserId(Orders order) {
        List<Orders> list = new ArrayList<>();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        StringBuffer sql = new StringBuffer();
        sql.append("select o.orderDate oorderDate, ");
        sql.append("o.orderId oorderId, ");
        sql.append("s.name sname ");
        sql.append("from orders o inner join setmeal s on o.smId = s.smId ");
        sql.append("where o.userId=? ");
        sql.append("and o.state=?");
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql.toString());
            pst.setString(1, order.getUserId());
            pst.setInt(2, order.getState());
            rs = pst.executeQuery();
            while (rs.next()) {
                Orders orders1 = new Orders();
                orders1.setOrderDate(rs.getString("oorderDate"));
                orders1.setOrderId(rs.getString("oorderId"));
                Setmeal setmeal = new Setmeal();
                setmeal.setName(rs.getString("sname"));
                orders1.setSetmeal(setmeal);
                list.add(orders1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {

            DBUtil.close(pst);

        }
        return list;
    }

}
